import { fileURLToPath, URL } from 'node:url'
import { resolve } from 'node:path'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import VueDevTools from 'vite-plugin-vue-devtools'

import Components from 'unplugin-vue-components/vite'
import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers'

import AutoImport from 'unplugin-auto-import/vite'

import vitePluginImp from 'vite-plugin-imp'

function pathResolve(dir: string) {
  //路径转化
  return resolve(__dirname, '.', dir)
}
// https://vitejs.dev/config/
const antdVueResolver = AntDesignVueResolver({ importStyle: 'less' })
export default defineConfig({
  plugins: [
    vue(),
    vueJsx(),
    VueDevTools(),
    AutoImport({
      imports: ['vue', 'vue-router'], // 自动导入vue和vue-router相关函数
      dts: 'src/auto-import.d.ts', // 生成 `auto-import.d.ts` 全局声明
      resolvers: [antdVueResolver]
    }),
    Components({
      // 指定组件所在文件夹的位置，默认是src/components
      dirs: ['src/components'],
      resolvers: [antdVueResolver],
      // 配置type文件生成位置
      dts: 'src/components.d.ts'
    }),
    vitePluginImp({
      libList: [
        {
          libName: 'ant-design-vue',
          style(name) {
            // use less
            return `ant-design-vue/es/${name}/style/index.js`
          }
        }
      ]
    })
  ],
  css: {
    preprocessorOptions: {
      less: {
        modifyVars: {
          // 在这里自定义主题色等样式
          'primary-color': '#1da57a',
          'link-color': '#1da57a',
          'border-radius-base': '2px'
        },
        javascriptEnabled: true
      }
    }
  },
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  }
})
